//
//  BNChoseMoodView.m
//  BeautifulNote
//
//  Created by Maker on 2019/7/17.
//  Copyright © 2019 Maker. All rights reserved.
//

#import "BNChoseMoodView.h"

@interface BNMoodColorCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *selectedIV;

@end


@implementation BNMoodColorCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews {
    [self addSubview:self.selectedIV];
    [self.selectedIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.height.mas_equalTo(20);
    }];
}

- (UIImageView *)selectedIV {
    if (!_selectedIV) {
        _selectedIV = [[UIImageView alloc] init];
        _selectedIV.contentMode = UIViewContentModeScaleAspectFit;
        _selectedIV.image = [UIImage imageNamed:@"chose"];
        _selectedIV.layer.masksToBounds = YES;
    }
    return _selectedIV;
}

@end


@interface BNChoseMoodView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray <UIColor *>*colorArray;
@property (nonatomic, strong) NSArray <NSString *>*contentArray;
@property (nonatomic, strong) UIButton *backgroundButton;
@property (nonatomic, strong) UIButton *certainButton;
@property (nonatomic, strong) UIImageView *contentView;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, copy) void(^block)(BNMoodType mood);
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *explainLb;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UICollectionView *collectionView;

@end


@implementation BNChoseMoodView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubViews];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.selectedIndex = 0;
        });
    }
    return self;
}


#pragma mark - action
- (void)clickCertainButton {
    if (self.block) {
        NSArray *tempArr = @[@(BNMoodTypeBlack),@(BNMoodTypeRed),@(BNMoodTypeOrange),@(BNMoodTypeYellow),@(BNMoodTypeGreen),@(BNMoodTypeQingse),@(BNMoodTypeBlue),@(BNMoodTypePurple),@(BNMoodTypeWhite),@(BNMoodTypeGray)];
        
        self.block([tempArr[self.selectedIndex] integerValue]);
    }
    [self removeFromSuperview];
}

- (void)didClickedBackground {
    [self.contentView.layer removeAllAnimations];
    [self removeFromSuperview];
}

#pragma mark - collectionViewDelegate & collectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.colorArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BNMoodColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BNMoodColorCell" forIndexPath:indexPath];
    cell.backgroundColor = self.colorArray[indexPath.item];
    if (indexPath.row == self.selectedIndex) {
        cell.selectedIV.alpha = 1.0f;
    } else {
        cell.selectedIV.alpha = 0.0f;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath.item;
}

#pragma mark- private method
- (void)addSubViews {
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [self addSubview:effectView];

    [self addSubview:self.backgroundButton];
    [self.backgroundButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth - 80);
        make.height.mas_equalTo(100);
    }];
    
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(10);
    }];
    
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(45);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(1);
    }];
    
    [self.contentView addSubview:self.certainButton];
    [self.certainButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.top.mas_equalTo(2);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(self.line.mas_bottom).mas_equalTo(10);
        make.height.mas_equalTo(30);
    }];
    
    [self.contentView addSubview:self.explainLb];
    [self.explainLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(self.collectionView.mas_bottom).mas_equalTo(15);
    }];
}

#pragma mark - publick method
+ (void)showMoodViewWithSelectedBlock:(void (^)(BNMoodType mood))block {
    BNChoseMoodView *moodView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    moodView.block = block;
    [[UIApplication sharedApplication].keyWindow addSubview:moodView];
}

#pragma mark - getter & setter

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    self.explainLb.text = self.contentArray[selectedIndex];
    self.explainLb.textColor = self.colorArray[selectedIndex];
    
    CGFloat height = [self.explainLb sizeThatFits:CGSizeMake(ScreenWidth - 100, MAXFLOAT)].height;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height + 125);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
    
    [self.collectionView reloadData];
}

- (NSArray<UIColor *> *)colorArray {
    if (!_colorArray) {
        _colorArray = @[blackMood, redMood, orangeMood, yellowMood, greenMood, qingseMood, blueMood, purpleMood, whiteMood, grayMood];
    }
    return _colorArray;
}

- (NSArray<NSString *> *)contentArray {
    if (!_contentArray) {
        _contentArray = @[@"严肃黑\n   黑色，代表认真，严肃，负责，神秘，庄重，低调和权威等特质。黑色一般为大多数白领人士所喜爱,当你需要表现出你的专业、同时又不想引人注目时,黑色是最安全的颜色",
                          @"热烈红\n   红色一般被认为是象征热情、自信和性感,是一种充满能量的色彩。喜欢红色的人大都非常自信、很有野心,会积极地去争取想得到的东西,属于精力充沛的行动派,对于任何事情都是激情高涨、永不言败",
                          @"温暖橙\n   橙色代表创造力、吸引力、野心、娱乐、 快乐、积极、平衡、华丽、慷慨、振奋、豪爽。融合了热情的红色和明媚的黄色的橙色，橙色是欢快活泼的光辉色彩，是暖色系中最温暖的色，它使人联想到金色的秋天，丰硕的果实，是一种富足、快乐而幸福的颜色",
                          @"活泼黄\n   黄色就像橙色和红色，黄色也是一个暖色。它有大自然、阳光、春天的涵义，而且通常被认为是一个快乐和有希望的色彩",
                          @"成长绿\n   绿色代表意义为清新、希望、安全、平静、舒适、生命、和平、宁静、自然、环保、成长、生机、青春，象征着和平和新生命",
                          @"信任青\n   青色象征着信任，坚强、希望、古朴和庄重，传统的器物和服饰常常采用青色。青色是中国特有的一种颜色，在中国古代社会中具有极其重要的意义。青是一种底色,清脆而不张扬,伶俐而不圆滑，清爽而不单调。青色，青和靛不同有波长可以区分。青色是在可见光谱中介于绿色和蓝色之间的颜色，有点类似于天空的颜色。",
                          @"清纯蓝\n   蓝色象征着清纯，简单，纯洁，快乐，蓝色还是一种理性与知性兼具的色彩,代表着博大的胸怀和沉稳的气质。喜欢蓝色的人大都性格沉着稳重,而且独立、诚实",
                          @"高贵紫\n   紫色爱情是高贵纯洁的爱情，它不看重俗气的金钱，唯一能打动它的也许只有从心而出的那份无可替代的真爱！紫色爱情是冷漠中蕴藏着火热的爱情，也许就如小王子中的狐狸一样。等待属于她的王子出现，然后就会随着他的脚步走出洞来，也或者这是一生都要在等待中度过的爱情",
                          @"干净白\n   白色代表着干净和平与神圣；保守、反动的性格；纯洁无瑕的爱情；公正、纯洁、端庄、正直、少壮、超脱凡尘与世俗的情感。",
                          @"平凡灰\n   白色代表着干净和平与神圣；保守、反动的性格；纯洁无瑕的爱情；公正、纯洁、端庄、正直、少壮、超脱凡尘与世俗的情感。"];
    }
    return _contentArray;
}

- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = bnColor(237, 237, 237);
    }
    return _line;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.font = mediumFont(16);
        _titleLb.textColor = [UIColor blackColor];
        _titleLb.textAlignment = NSTextAlignmentCenter;
        _titleLb.text = @"选择心情";
    }
    return _titleLb;
}

- (UILabel *)explainLb {
    if (!_explainLb) {
        _explainLb = [UILabel new];
        _explainLb.font = lightFont(16);
        _explainLb.numberOfLines = 0;
        _explainLb.textColor = [UIColor blackColor];
    }
    return _explainLb;
}

- (UIButton *)backgroundButton {
    if (!_backgroundButton) {
        _backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backgroundButton addTarget:self action:@selector(didClickedBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backgroundButton;
}

- (UIImageView *)contentView {
    if (!_contentView) {
        _contentView = [UIImageView new];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 6;
        _contentView.layer.masksToBounds = YES;
        _contentView.image = [UIImage imageNamed:@"moodBGV"];
        _contentView.contentMode = UIViewContentModeScaleAspectFill;
        _contentView.userInteractionEnabled = YES;
    }
    return _contentView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(30, 30);
        layout.minimumLineSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:BNMoodColorCell.class forCellWithReuseIdentifier:@"BNMoodColorCell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

- (UIButton *)certainButton {
    if (!_certainButton) {
        _certainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_certainButton setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
        [_certainButton addTarget:self action:@selector(clickCertainButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _certainButton;
}
@end
